package cn.iocoder.yudao.module.promotion.convert.quickcombinationactivity;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.combination.vo.activity.CombinationActivityPageItemRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.combination.vo.product.CombinationProductBaseVO;
import cn.iocoder.yudao.module.promotion.controller.admin.quickcombinationactivity.vo.product.QuickCombinationProductBaseVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationProductDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.quickcombinationactivity.QuickCombinationProductDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import cn.iocoder.yudao.module.promotion.controller.admin.quickcombinationactivity.vo.*;
import cn.iocoder.yudao.module.promotion.dal.dataobject.quickcombinationactivity.QuickCombinationActivityDO;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;

/**
 * 进货活动 Convert
 *
 * @author 曹建伟
 */
@Mapper
public interface QuickCombinationActivityConvert {

    QuickCombinationActivityConvert INSTANCE = Mappers.getMapper(QuickCombinationActivityConvert.class);

    QuickCombinationActivityDO convert(QuickCombinationActivityCreateReqVO bean);

    QuickCombinationActivityDO convert(QuickCombinationActivityUpdateReqVO bean);

    QuickCombinationActivityRespVO convert(QuickCombinationActivityDO bean);

    List<QuickCombinationActivityRespVO> convertList(List<QuickCombinationActivityDO> list);

    PageResult<QuickCombinationActivityRespVO> convertPage(PageResult<QuickCombinationActivityDO> page);

    List<QuickCombinationActivityExcelVO> convertList02(List<QuickCombinationActivityDO> list);

    @Mappings({
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "activityId", source = "activity.id"),
            @Mapping(target = "spuId", source = "activity.spuId"),
            @Mapping(target = "skuId", source = "product.skuId"),
            @Mapping(target = "combinationPrice", source = "product.combinationPrice")
    })
    QuickCombinationProductDO convert(QuickCombinationActivityDO activity, QuickCombinationProductBaseVO product);

    default List<QuickCombinationProductDO> convertList(List<? extends QuickCombinationProductBaseVO> products, QuickCombinationActivityDO activity) {
        return CollectionUtils.convertList(products, item -> convert(activity, item));
    }

    default PageResult<QuickCombinationActivityRespVO> convertPage(PageResult<QuickCombinationActivityDO> page, List<ProductSpuRespDTO> spuList) {
        PageResult<QuickCombinationActivityRespVO> pageResult = convertPage(page);

        Map<Long, ProductSpuRespDTO> spuMap = convertMap(spuList, ProductSpuRespDTO::getId);
        pageResult.getList().forEach(item -> {
            MapUtils.findAndThen(spuMap, item.getSpuId(), spu -> item.setSpuName(spu.getName()).setPicUrl(spu.getPicUrl()));
        });
        return pageResult;
    }

}
